<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./OL_SDK/include-openlayers-local.js"></script>
    <script src="./libs/gaode.js"></script>
</head>

<body>
    <button id="active" onclick="activeDraw()">激活画笔</button>
    <button onclick="removeDraw()">移除画笔+清空画布</button>
    <div id="map">
    </div>
    <script>
        var map = new ol.Map({
            target: "map",
            layers: [gaode],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [114.30, 30.50],
                zoom: 4
            })
        })
        var btn = document.getElementById("active");
        /* 1、创建画布 */
        let source = new ol.source.Vector({

        })
        let layer = new ol.layer.Vector({
            source
        })
        map.addLayer(layer)
        let draw = null
        function activeDraw() {
            /* 设置样式 */
            btn.disabled = true;
            btn.style.cursor = "no-drop"
            draw = new ol.interaction.Draw({
                type: "Point",
                source
            })
            map.addInteraction(draw);
            draw.on("drawend", () => {
                // console.log("draw")
                /* 移除画笔 */
                map.removeInteraction(draw)
                draw = null
                /* 恢复样式 */
                btn.disabled = false;
                btn.style.cursor = "default"
            })
        }
        function removeDraw() {
            /* 1、清除画笔 */
            if (draw) {
                map.getInteractions().pop()
                draw = null
            }
            /* 2、清空画布 */
            source.clear();
            /* 恢复样式 */
            btn.disabled = false;
            btn.style.cursor = "default"
        }
    </script>
</body>

</html>